package com.kfm.goods.provider;

import com.kfm.goods.domain.User;
import org.apache.ibatis.jdbc.SQL;

/**
 * 用户表【user】的SQL提供
 */
public class UserSQLProvider {

    public String selectAll2(final User user){
        return new SQL(){
            // 实例初始化器
            {
                // 要查询的列
                SELECT("*");
                FROM("user");
                // 动态拼接where条件
                if (user.getId() != null){
                    WHERE("id = #{id}");
                }
                if (user.getUsername() != null && !"".equals(user.getUsername())){
                    WHERE("username = #{username}");
                }
                if (user.getPassword() != null){
                    WHERE("password = #{password}");
                }
                if (user.getName() != null && !"".equals(user.getName())){
                    WHERE("name = #{name}");
                }
            }
        }.toString();
    }

    public String insertSelective(final User user){
        return new SQL(){
            {
                INSERT_INTO("user");
                if (user.getId() != null){
                    VALUES("id", "#{id}");
                }
                if (user.getUsername() != null){
                    VALUES("username", "#{username}");
                }
                if (user.getPassword() != null){
                    VALUES("password", "#{password}");
                }
                if (user.getName() != null){
                    VALUES("name", "#{name}");
                }
            }
        }.toString();
    }

    public String updateByPrimaryKeySelective(final User user){
        return new SQL(){
            {
                UPDATE("user");
                if (user.getId() != null){
                    WHERE("id = #{id}");
                }
                if (user.getUsername() != null){
                    SET("username = #{username}");
                }
                if (user.getPassword() != null){
                    SET("password = #{password}");
                }
                if (user.getName() != null){
                    SET("name = #{name}");
                }
            }
        }.toString();
    }
}
